﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using _01.NumbersOccurrences;

namespace _02.EvenOccurrencesNumbers
{
    class TestGenerator
    {
        static Random random;
        const int minLength = 100000;
        const int maxLength = 110000;
        const int minMember = -500;
        const int maxMember = 2000;

        public static void CreateTest(string inputFileName, string outputFileName)
        {
            string sequence = ReadInput(@"..\..\Tests\" + inputFileName);
            //string sequence = CreateSequence();
            //PrintInput(@"..\..\Tests\" + inputFileName, sequence);

            List<int> numbers = new List<int>(EvenOccurrencesNumbers.ReadInput(sequence));

            Dictionary<int, int> numberOccurTimesCounter = EvenOccurrencesNumbers.OccurrencesCounter(numbers.ToArray());

            numbers = EvenOccurrencesNumbers.EvenOccurrencesNumbersList(numbers, numberOccurTimesCounter);

            //EvenOccurrencesNumbers.RemoveOddOccurrencesNumbers(numbers, numberOccurTimesCounter);

            //NumbersOccurrences.PrintConsole(numberOccurTimesCounter);
            PrintOutput(@"..\..\Tests\" + outputFileName, numbers);
        }

        private static string ReadInput(string fileName)
        {
            StreamReader reader = new StreamReader(fileName);
            string sequence;
            using (reader)
            {
                sequence = reader.ReadLine();
            }
            return sequence;
        }

        public static string CreateSequence()
        {
            random = new Random();
            StringBuilder sequence = new StringBuilder();
            int sequenceLength = random.Next(minLength, maxLength);

            int member = random.Next(minMember, maxMember);
            for (int i = 0; i < sequenceLength - 1; i++)
            {
                sequence.Append(member + " ");
                //sequence.Append(member + " ");
                member = random.Next(minMember, maxMember);
            }
            sequence.Append(member);

            return sequence.ToString();
        }

        private static void PrintInput(string fileName, string sequence)
        {
            StreamWriter writer = new StreamWriter(fileName);

            using (writer)
            {
                writer.WriteLine(sequence);
            }
        }

        public static void PrintOutput(string fileName, List<int> numbers)
        {
            StreamWriter writer = new StreamWriter(fileName);

            using (writer)
            {
                if (numbers.Count > 0)
                {
                    for (int i = 0; i < numbers.Count - 1; i++)
                    {
                        writer.Write("{0} ", numbers[i]);
                    }
                    writer.WriteLine(numbers[numbers.Count - 1]);
                }
            }
        }
    }
}
